package nonthreaded;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * Normal version of the page retriever.
 * Keeps track of URLs in a queue that need to be parsed and analyzed.
 * Filters out URLs that have already been retrieved.
 * @author Daniel Anderson and Luc H. Le
 * @version TCSS422 2012
 */
public class PageRetriever {
	
	/**URLs to be parsed and analyzed*/
	Deque<URL> my_url_queue;
	
	/**URLs that have been retrieved in the past.*/
	List<URL> my_old_urls;
	
	/**Number of URLs retrieved.*/
	int my_retrieved_count;
	
	/**Constructor for PageRetriever.*/
	public PageRetriever() {
		my_url_queue = new ArrayDeque<URL>();
		my_old_urls = new ArrayList<URL>();
		my_retrieved_count = 0;
	}//end of constr
	
	/**Get method for returning the number of retrieved pages so far.*/
	public int getRetrievedCount() {
		return my_retrieved_count;
	}//end get
	
	
	/**Checks if any URLs exist in the deque*/
	public boolean isEmpty() {
		return my_url_queue.isEmpty();
	}//end of isEmpty
	
	/**Removes front element of Deque and returns it.*/
	public URL getNextURL() throws IOException {
		
		return my_url_queue.remove();
	}//end of get
	
	//TODO this is the slowest point of the single threaded webcrawler.
	/**Add a single URL to the Deque.*/ 
	public void addURL(URL url) {
		if(!my_old_urls.contains(url)) {
			my_old_urls.add(url);
			my_url_queue.add(url);
			my_retrieved_count++;
		}
	}//end of addURL
	
	/**Add many URLs to the Deque*/
	public void addURLs(List<URL> urls) {
		for(URL u : urls) {
			if(!my_old_urls.contains(u) && u != null) {
				my_retrieved_count++;
				try {
					if(((HttpURLConnection) u.openConnection()).getResponseCode() != 403 ) {
						my_old_urls.add(u);
						my_url_queue.add(u);
					}
				} catch (Exception e) {
					//System.out.println(u + " was a dud");
					//Unhandled URL type.
				}
			}
		}
	}//end of addURLs
}//end of PageRetriever
